#include <iostream>
#include<queue>
//#include<vector>
using namespace std;

int main() {
    int n;
    cin>>n;
    long long lenth=0;
    priority_queue<long long,vector<long long>,greater<long long>> heap;
    while(n--)
    {
        int a;
        cin>>a;
        heap.push(a);
    }
    while(heap.size()!=1)
    {
        long long tmp1=heap.top();heap.pop();
        long long tmp2=heap.top();heap.pop();
        lenth+=tmp1+tmp2;
        heap.push(tmp1+tmp2);
    }

    cout<<lenth;
    return 0;

}